package com.jwthhealth_pub.old.common.location;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.LogUtil;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.LOCATION_SERVICE;
import static android.support.v4.content.PermissionChecker.checkSelfPermission;

/**
 * 定位工具类
 * Created by Administrator on 2017/7/13.
 */

public class LocationUtil {
    public static List<Double> getLocation(){
        List<Double> list = new ArrayList<>();

        LocationManager locationManager = (LocationManager) App.mContext.getSystemService(LOCATION_SERVICE);
        //获取所有的定位方式，true:表示返回所有可用定位方式
        List<String> providers = locationManager.getProviders(true);
        Log.d("LocationUtil", "providers.size():" + providers.size());
        for (String string : providers) {
            LogUtil.i("LocationUtil", string);
        }
        //获取最佳的定位方式
        Criteria criteria = new Criteria();
        criteria.setAltitudeRequired(true);//设置是否可以定位海拔,如果设置定位海拔，返回一定是gps
        //criteria : 设置定位属性
        //enabledOnly : true如果定位可用就返回
        String bestProvider = locationManager.getBestProvider(criteria, true);

        if (checkSelfPermission(App.mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(App.mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            LogUtil.e("LocationUtil", "no location permission");
            return null;
        }


        Location mLocation = locationManager.getLastKnownLocation(bestProvider);//需要权限检测，上面添加

        if (mLocation == null) {
            locationManager.requestLocationUpdates(bestProvider, 0, 0, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Log.d("LocationUtil", "location.getAltitude():" + location.getAltitude());
                    Log.d("LocationUtil", "location.getLongitude():" + location.getLongitude());
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }
            });
            LogUtil.e("LocationUtil", "mlocation is null");
            return null;
        }

        double mLat = mLocation.getLatitude();//获取纬度
        double mLng = mLocation.getLongitude();//获取经度

        list.add(mLat);
        list.add(mLng);

        return list;
    }

//    实时获取定位信息
//    public static void getLocation() {
//        MyLocationListener myLocationListener = new MyLocationListener();
//        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, myLocationListener);
//    }

    //移动定位监听，监听不到
//    private class MyLocationListener implements LocationListener {
//        //当定位位置改变的调用的方法
//        //Location : 当前的位置
//        @Override
//        public void onLocationChanged(Location location) {
//            Log.d("SubscribePresenter", "here1");
//            location.getAccuracy();//获取精确位置
//            location.getAltitude();//获取海拔
//            double latitude = location.getLatitude();//获取纬度，平行于赤道
//            double longitude = location.getLongitude();//获取经度，垂直于赤道
//            LogUtil.i("SubscribePresenter","longitude:"+longitude+"  latitude:"+latitude);
//        }
//        //当定位状态发生改变的时候调用的方式
//        @Override
//        public void onStatusChanged(String provider, int status, Bundle extras) {
//            // TODO Auto-generated method stub
//            Log.d("SubscribePresenter", "here2");
//
//        }
//        //当定位可用的时候调用的方法
//        @Override
//        public void onProviderEnabled(String provider) {
//            // TODO Auto-generated method stub
//            Log.d("SubscribePresenter", "here3");
//
//        }
//        //当定位不可用的时候调用的方法
//        @Override
//        public void onProviderDisabled(String provider) {
//            // TODO Auto-generated method stub
//            Log.d("SubscribePresenter", "here4");
//
//        }
//    }
}
